package com.zhxt.web.controller.reportapi;

import com.zhxt.common.annotation.Log;
import com.zhxt.common.constant.HttpStatus;
import com.zhxt.common.core.controller.BaseController;
import com.zhxt.common.core.domain.AjaxResult;
import com.zhxt.common.core.page.TableDataInfo;
import com.zhxt.common.enums.BusinessType;
import com.zhxt.system.domain.ZhxtFiles;
import com.zhxt.system.domain.ZhxtSourceInfo;
import com.zhxt.system.service.IZhxtFilesService;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 文件处理，包含管理制度和电子期刊
 */

@RestController
@RequestMapping("/repapi/files")
public class FilesController extends BaseController {

    @Autowired
    private IZhxtFilesService iZhxtFilesService;
    /**
     * 查询管理制度列表
     */
    @Log(title = "大屏-管理制度查询", businessType = BusinessType.OTHER)
    @PostMapping("/getListM")
    public TableDataInfo getListM(ZhxtFiles zhxtFiles){
        ZhxtFilesXss(zhxtFiles);
        startPage();
        zhxtFiles.setSourcestr("0");
        List<ZhxtFiles> zhxtFilesList=iZhxtFilesService.getListM(zhxtFiles);
        if(zhxtFilesList.isEmpty()){
            TableDataInfo tableDataInfo = new TableDataInfo();
            tableDataInfo.setCode(HttpStatus.NOT_FOUND);
            tableDataInfo.setMsg("查询失败");
            return tableDataInfo;
        }
        return getDataTable(zhxtFilesList);
    }

    public ZhxtFiles ZhxtFilesXss(ZhxtFiles zhxtFiles){
        zhxtFiles.setDeptid(StringEscapeUtils.escapeHtml4(zhxtFiles.getDeptid()));
        zhxtFiles.setImgurl(StringEscapeUtils.escapeHtml4(zhxtFiles.getImgurl()));
        zhxtFiles.setSourcestr(StringEscapeUtils.escapeHtml4(zhxtFiles.getSourcestr()));
        zhxtFiles.setFiledesc(StringEscapeUtils.escapeHtml4(zhxtFiles.getFiledesc()));
        zhxtFiles.setFilename(StringEscapeUtils.escapeHtml4(zhxtFiles.getFilename()));
        zhxtFiles.setFiletype(StringEscapeUtils.escapeHtml4(zhxtFiles.getFiletype()));
        zhxtFiles.setFileurl(StringEscapeUtils.escapeHtml4(zhxtFiles.getFileurl()));
        zhxtFiles.setFilestatus(StringEscapeUtils.escapeHtml4(zhxtFiles.getFilestatus()));
        zhxtFiles.setCtype(StringEscapeUtils.escapeHtml4(zhxtFiles.getCtype()));
        zhxtFiles.setFilenum(StringEscapeUtils.escapeHtml4(zhxtFiles.getFilenum()));
        zhxtFiles.setStatus(StringEscapeUtils.escapeHtml4(zhxtFiles.getStatus()));
        return zhxtFiles;
    }
    /**
     * 查询管理制度详情
     */
    @PostMapping("/getMInfo")
    public AjaxResult getMInfo(String contnum){
        contnum=StringEscapeUtils.escapeHtml4(contnum);
        AjaxResult ajax=AjaxResult.success();
        List<ZhxtSourceInfo> zhxtSourceInfoList=iZhxtFilesService.getMInfo(contnum);
        if (zhxtSourceInfoList != null && !zhxtSourceInfoList.isEmpty()) {
            // 对返回的列表进行XSS过滤
            zhxtSourceInfoList.forEach(tj -> {
                tj.setCtype(StringEscapeUtils.escapeHtml4(tj.getCtype()));
                tj.setImgurl(StringEscapeUtils.escapeHtml4(tj.getImgurl()));
                tj.setSourcenum(StringEscapeUtils.escapeHtml4(tj.getSourcenum()));
                tj.setNamedesc(StringEscapeUtils.escapeHtml4(tj.getNamedesc()));
            });
            ajax.put("data",zhxtSourceInfoList);
        }
        if(!(zhxtSourceInfoList == null && zhxtSourceInfoList.isEmpty())){
            //登记浏览次数及行为
            int lookid=iZhxtFilesService.openMFile(contnum);
            ajax.put("lookid",lookid);
        }
        List<ZhxtSourceInfo> zhxtSourceInfoListf=iZhxtFilesService.getFInfo(contnum);
        if (zhxtSourceInfoListf != null && !zhxtSourceInfoListf.isEmpty()) {
            // 对返回的列表进行XSS过滤
            zhxtSourceInfoListf.forEach(tj -> {
                tj.setNamedesc(StringEscapeUtils.escapeHtml4(tj.getNamedesc()));
                tj.setSourcenum(StringEscapeUtils.escapeHtml4(tj.getSourcenum()));
                tj.setCtype(StringEscapeUtils.escapeHtml4(tj.getCtype()));
                tj.setImgurl(StringEscapeUtils.escapeHtml4(tj.getImgurl()));
            });
            ajax.put("fdata",zhxtSourceInfoListf);
        }
        return ajax;
    }

    /**
     * 关闭管理制度查看
     */
    @PostMapping("/closeMInfo")
    public AjaxResult closeMInfo(int lookid){
        AjaxResult ajax=AjaxResult.success();
        int rNum=iZhxtFilesService.closeMFile(lookid);
        if(rNum > 0){
            ajax.put("dcode","00");
            ajax.put("dresult","关闭成功");
        }else{
            ajax.put("dcode","99");
            ajax.put("dresult","关闭失败");
        }
        return ajax;
    }

    /**
     * 查询电子期刊列表
     */
    @Log(title = "大屏-电子期刊查询", businessType = BusinessType.OTHER)
    @PostMapping("/getListT")
    public TableDataInfo getListT(ZhxtFiles zhxtFiles){
        ZhxtFilesXss(zhxtFiles);
        startPage();
        List<ZhxtFiles> zhxtFilesList=iZhxtFilesService.getListT(zhxtFiles);
        return getDataTable(zhxtFilesList);
    }
    /**
     * 查询电子期刊详情
     */
    @PostMapping("/getTInfo")
    public AjaxResult getTInfo(String contnum){
        contnum=StringEscapeUtils.escapeHtml4(contnum);
        AjaxResult ajax=AjaxResult.success();
        List<ZhxtSourceInfo> zhxtSourceInfoList=iZhxtFilesService.getTInfo(contnum);
        ajax.put("data",zhxtSourceInfoList);
        return ajax;
    }
}
